home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zdevice2.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  9.4 KB  |  374 lines

  1. /* Copyright (C) 1993, 1995, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zdevice2.c,v 1.3 2000/09/19 19:00:53 lpd Exp $ */
  20. /* Level 2 device operators */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "oper.h"
  25. #include "dstack.h"        /* for dict_find_name */
  26. #include "estack.h"
  27. #include "idict.h"
  28. #include "idparam.h"
  29. #include "igstate.h"
  30. #include "iname.h"
  31. #include "iutil.h"
  32. #include "store.h"
  33. #include "gxdevice.h"
  34. #include "gsstate.h"
  35.  
  36. /* Forward references */
  37. private int z2copy_gstate(P1(i_ctx_t *));
  38. private int push_callout(P2(i_ctx_t *, const char *));
  39.  
  40. /* Extend the `copy' operator to deal with gstates. */
  41. /* This is done with a hack -- we know that gstates are the only */
  42. /* t_astruct subtype that implements copy. */
  43. /* We export this for recognition in FunctionType 4 functions. */
  44. int
  45. z2copy(i_ctx_t *i_ctx_p)
  46. {
  47.     os_ptr op = osp;
  48.     int code = zcopy(i_ctx_p);
  49.  
  50.     if (code >= 0)
  51.     return code;
  52.     if (!r_has_type(op, t_astruct))
  53.     return code;
  54.     return z2copy_gstate(i_ctx_p);
  55. }
  56.  
  57. /* - .currentshowpagecount <count> true */
  58. /* - .currentshowpagecount false */
  59. private int
  60. zcurrentshowpagecount(i_ctx_t *i_ctx_p)
  61. {
  62.     os_ptr op = osp;
  63.     gx_device *dev = gs_currentdevice(igs);
  64.  
  65.     if ((*dev_proc(dev, get_page_device))(dev) == 0) {
  66.     push(1);
  67.     make_false(op);
  68.     } else {
  69.     push(2);
  70.     make_int(op - 1, dev->ShowpageCount);
  71.     make_true(op);
  72.     }
  73.     return 0;
  74. }
  75.  
  76. /* - .currentpagedevice <dict> <bool> */
  77. private int
  78. zcurrentpagedevice(i_ctx_t *i_ctx_p)
  79. {
  80.     os_ptr op = osp;
  81.     gx_device *dev = gs_currentdevice(igs);
  82.  
  83.     push(2);
  84.     if ((*dev_proc(dev, get_page_device))(dev) != 0) {
  85.     op[-1] = istate->pagedevice;
  86.     make_true(op);
  87.     } else {
  88.     make_null(op - 1);
  89.     make_false(op);
  90.     }
  91.     return 0;
  92. }
  93.  
  94. /* <local_dict|null> .setpagedevice - */
  95. private int
  96. zsetpagedevice(i_ctx_t *i_ctx_p)
  97. {
  98.     os_ptr op = osp;
  99.     int code;
  100.  
  101. /******
  102.     if ( igs->in_cachedevice )
  103.     return_error(e_undefined);
  104.  ******/
  105.     if (r_has_type(op, t_dictionary)) {
  106.     check_dict_read(*op);
  107. #if 0    /****************/
  108.     /*
  109.      * In order to avoid invalidaccess errors on setpagedevice,
  110.      * the dictionary must be allocated in local VM.
  111.      */
  112.     if (!(r_is_local(op)))
  113.         return_error(e_invalidaccess);
  114. #endif    /****************/
  115.     /* Make the dictionary read-only. */
  116.     code = zreadonly(i_ctx_p);
  117.     if (code < 0)
  118.         return code;
  119.     } else {
  120.     check_type(*op, t_null);
  121.     }
  122.     istate->pagedevice = *op;
  123.     pop(1);
  124.     return 0;
  125. }
  126.  
  127. /* Default Install/BeginPage/EndPage procedures */
  128. /* that just call the procedure in the device. */
  129.  
  130. /* - .callinstall - */
  131. private int
  132. zcallinstall(i_ctx_t *i_ctx_p)
  133. {
  134.     gx_device *dev = gs_currentdevice(igs);
  135.  
  136.     if ((dev = (*dev_proc(dev, get_page_device))(dev)) != 0) {
  137.     int code = (*dev->page_procs.install) (dev, igs);
  138.  
  139.     if (code < 0)
  140.         return code;
  141.     }
  142.     return 0;
  143. }
  144.  
  145. /* <showpage_count> .callbeginpage - */
  146. private int
  147. zcallbeginpage(i_ctx_t *i_ctx_p)
  148. {
  149.     os_ptr op = osp;
  150.     gx_device *dev = gs_currentdevice(igs);
  151.  
  152.     check_type(*op, t_integer);
  153.     if ((dev = (*dev_proc(dev, get_page_device))(dev)) != 0) {
  154.     int code = (*dev->page_procs.begin_page)(dev, igs);
  155.  
  156.     if (code < 0)
  157.         return code;
  158.     }
  159.     pop(1);
  160.     return 0;
  161. }
  162.  
  163. /* <showpage_count> <reason_int> .callendpage <flush_bool> */
  164. private int
  165. zcallendpage(i_ctx_t *i_ctx_p)
  166. {
  167.     os_ptr op = osp;
  168.     gx_device *dev = gs_currentdevice(igs);
  169.     int code;
  170.  
  171.     check_type(op[-1], t_integer);
  172.     check_type(*op, t_integer);
  173.     if ((dev = (*dev_proc(dev, get_page_device))(dev)) != 0) {
  174.     code = (*dev->page_procs.end_page)(dev, (int)op->value.intval, igs);
  175.     if (code < 0)
  176.         return code;
  177.     if (code > 1)
  178.         return_error(e_rangecheck);
  179.     } else {
  180.     code = (op->value.intval == 2 ? 0 : 1);
  181.     }
  182.     make_bool(op - 1, code);
  183.     pop(1);
  184.     return 0;
  185. }
  186.  
  187. /* ------ Wrappers for operators that save the graphics state. ------ */
  188.  
  189. /* When saving the state with the current device a page device, */
  190. /* we need to make sure that the page device dictionary exists */
  191. /* so that grestore can use it to reset the device parameters. */
  192. /* This may have significant performance consequences, but we don't see */
  193. /* any way around it. */
  194.  
  195. /* Check whether we need to call out to create the page device dictionary. */
  196. private bool
  197. save_page_device(gs_state *pgs)
  198. {
  199.     return 
  200.     (r_has_type(&gs_int_gstate(pgs)->pagedevice, t_null) &&
  201.      (*dev_proc(gs_currentdevice(pgs), get_page_device))(gs_currentdevice(pgs)) != 0);
  202. }
  203.  
  204. /* - gsave - */
  205. private int
  206. z2gsave(i_ctx_t *i_ctx_p)
  207. {
  208.     if (!save_page_device(igs))
  209.     return gs_gsave(igs);
  210.     return push_callout(i_ctx_p, "%gsavepagedevice");
  211. }
  212.  
  213. /* - save - */
  214. private int
  215. z2save(i_ctx_t *i_ctx_p)
  216. {
  217.     if (!save_page_device(igs))
  218.     return zsave(i_ctx_p);
  219.     return push_callout(i_ctx_p, "%savepagedevice");
  220. }
  221.  
  222. /* - gstate <gstate> */
  223. private int
  224. z2gstate(i_ctx_t *i_ctx_p)
  225. {
  226.     if (!save_page_device(igs))
  227.     return zgstate(i_ctx_p);
  228.     return push_callout(i_ctx_p, "%gstatepagedevice");
  229. }
  230.  
  231. /* <gstate1> <gstate2> copy <gstate2> */
  232. private int
  233. z2copy_gstate(i_ctx_t *i_ctx_p)
  234. {
  235.     if (!save_page_device(igs))
  236.     return zcopy_gstate(i_ctx_p);
  237.     return push_callout(i_ctx_p, "%copygstatepagedevice");
  238. }
  239.  
  240. /* <gstate> currentgstate <gstate> */
  241. private int
  242. z2currentgstate(i_ctx_t *i_ctx_p)
  243. {
  244.     if (!save_page_device(igs))
  245.     return zcurrentgstate(i_ctx_p);
  246.     return push_callout(i_ctx_p, "%currentgstatepagedevice");
  247. }
  248.  
  249. /* ------ Wrappers for operators that reset the graphics state. ------ */
  250.  
  251. /* Check whether we need to call out to restore the page device. */
  252. private bool
  253. restore_page_device(const gs_state * pgs_old, const gs_state * pgs_new)
  254. {
  255.     gx_device *dev_old = gs_currentdevice(pgs_old);
  256.     gx_device *dev_new;
  257.     gx_device *dev_t1;
  258.     gx_device *dev_t2;
  259.  
  260.     if ((dev_t1 = (*dev_proc(dev_old, get_page_device)) (dev_old)) == 0)
  261.     return false;
  262.     dev_new = gs_currentdevice(pgs_new);
  263.     if (dev_old != dev_new) {
  264.     if ((dev_t2 = (*dev_proc(dev_new, get_page_device)) (dev_new)) == 0)
  265.         return false;
  266.     if (dev_t1 != dev_t2)
  267.         return true;
  268.     }
  269.     /*
  270.      * The current implementation of setpagedevice just sets new
  271.      * parameters in the same device object, so we have to check
  272.      * whether the page device dictionaries are the same.
  273.      */
  274.     return !obj_eq(&gs_int_gstate(pgs_old)->pagedevice,
  275.            &gs_int_gstate(pgs_new)->pagedevice);
  276. }
  277.  
  278. /* - grestore - */
  279. private int
  280. z2grestore(i_ctx_t *i_ctx_p)
  281. {
  282.     if (!restore_page_device(igs, gs_state_saved(igs)))
  283.     return gs_grestore(igs);
  284.     return push_callout(i_ctx_p, "%grestorepagedevice");
  285. }
  286.  
  287. /* - grestoreall - */
  288. private int
  289. z2grestoreall(i_ctx_t *i_ctx_p)
  290. {
  291.     for (;;) {
  292.     if (!restore_page_device(igs, gs_state_saved(igs))) {
  293.         bool done = !gs_state_saved(gs_state_saved(igs));
  294.  
  295.         gs_grestore(igs);
  296.         if (done)
  297.         break;
  298.     } else
  299.         return push_callout(i_ctx_p, "%grestoreallpagedevice");
  300.     }
  301.     return 0;
  302. }
  303.  
  304. /* <save> restore - */
  305. private int
  306. z2restore(i_ctx_t *i_ctx_p)
  307. {
  308.     for (;;) {
  309.     if (!restore_page_device(igs, gs_state_saved(igs))) {
  310.         if (!gs_state_saved(gs_state_saved(igs)))
  311.         break;
  312.         gs_grestore(igs);
  313.     } else
  314.         return push_callout(i_ctx_p, "%restorepagedevice");
  315.     }
  316.     return zrestore(i_ctx_p);
  317. }
  318.  
  319. /* <gstate> setgstate - */
  320. private int
  321. z2setgstate(i_ctx_t *i_ctx_p)
  322. {
  323.     os_ptr op = osp;
  324.  
  325.     check_stype(*op, st_igstate_obj);
  326.     if (!restore_page_device(igs, igstate_ptr(op)))
  327.     return zsetgstate(i_ctx_p);
  328.     return push_callout(i_ctx_p, "%setgstatepagedevice");
  329. }
  330.  
  331. /* ------ Initialization procedure ------ */
  332.  
  333. const op_def zdevice2_l2_op_defs[] =
  334. {
  335.     op_def_begin_level2(),
  336.     {"0.currentshowpagecount", zcurrentshowpagecount},
  337.     {"0.currentpagedevice", zcurrentpagedevice},
  338.     {"1.setpagedevice", zsetpagedevice},
  339.         /* Note that the following replace prior definitions */
  340.         /* in the indicated files: */
  341.     {"1copy", z2copy},        /* zdps1.c */
  342.     {"0gsave", z2gsave},    /* zgstate.c */
  343.     {"0save", z2save},        /* zvmem.c */
  344.     {"0gstate", z2gstate},    /* zdps1.c */
  345.     {"1currentgstate", z2currentgstate},    /* zdps1.c */
  346.     {"0grestore", z2grestore},    /* zgstate.c */
  347.     {"0grestoreall", z2grestoreall},    /* zgstate.c */
  348.     {"1restore", z2restore},    /* zvmem.c */
  349.     {"1setgstate", z2setgstate},    /* zdps1.c */
  350.         /* Default Install/BeginPage/EndPage procedures */
  351.         /* that just call the procedure in the device. */
  352.     {"0.callinstall", zcallinstall},
  353.     {"1.callbeginpage", zcallbeginpage},
  354.     {"2.callendpage", zcallendpage},
  355.     op_def_end(0)
  356. };
  357.  
  358. /* ------ Internal routines ------ */
  359.  
  360. /* Call out to a PostScript procedure. */
  361. private int
  362. push_callout(i_ctx_t *i_ctx_p, const char *callout_name)
  363. {
  364.     int code;
  365.  
  366.     check_estack(1);
  367.     code = name_enter_string(callout_name, esp + 1);
  368.     if (code < 0)
  369.     return code;
  370.     ++esp;
  371.     r_set_attrs(esp, a_executable);
  372.     return o_push_estack;
  373. }
  374.